package com.sxbbc.common.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.core.entity.jz.SysCashRule;
import com.sxbbc.common.core.mapper.jz.SysCashRuleMapper;
import com.sxbbc.common.core.service.ISysCashRuleService;

/**
 * <p>
 * 平台提现规则 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-04-30
 */
@Service
public class SysCashRuleServiceImpl extends ServiceImpl<SysCashRuleMapper, SysCashRule> implements ISysCashRuleService {

    @Override
    public RestResponse editCashSetting(SysCashRule cashRule) {
        if (cashRule.getNum() == null) {
            return GetRest.getFail("请填写提现次数");
        }
        if (cashRule.getScale() == null) {
            return GetRest.getFail("请填写提现手续费");
        }
        if (cashRule.getMinVal() == null) {
            return GetRest.getFail("请填写提现最低额");
        }
        if (cashRule.getMaxVal() == null) {
            return GetRest.getFail("请填写提现最高额");
        }
        if (cashRule.getIsOpen() == null) {
            return GetRest.getFail("请选择是否开启提现");
        }
        if (cashRule.getType() == null) {
            return GetRest.getFail("请选择提现限制类型");
        }
        boolean save = super.saveOrUpdate(cashRule);
        if (!save) {
            throw new ThrowJsonException("保存提现规则失败");
        }
        return GetRest.getSuccess("保存成功");
    }

	@Override
	public Map<String, Object> getCashRule(Integer type) {
		Map<String, Object> map = new HashMap<>();
		SysCashRule queryCashRule = new SysCashRule();
		queryCashRule.setRoleType(type);
		List<SysCashRule> list = super.list(queryWrapper(queryCashRule));
		if (list == null || list.size() <= 0) {
			return map;	
		}
		SysCashRule cashRule = list.get(0);
		map.put("scale", cashRule.getScale());
		map.put("minVal", cashRule.getMinVal());
		map.put("maxVal", cashRule.getMaxVal());
		map.put("multiple", cashRule.getMultiple());
		return map;
	}
	
	@Override
	public QueryWrapper<SysCashRule> queryWrapper (SysCashRule cashRule){
		QueryWrapper<SysCashRule> wrapper = new QueryWrapper<SysCashRule>();
		if (cashRule.getRoleType() != null) {
			wrapper.eq("role_type", cashRule.getRoleType());
		}
		return wrapper;
	}
}
